using System;
using System.Collections.Generic;
using System.Text;

namespace AUSRIS.Reports.Utilities.Deployment.Entities
{
	public class ItemPolicy : IComparable, IComparable<ItemPolicy>
	{
		#region Private Fields

		private string groupUserName;
		private IList<ItemPolicyRole> roles;

		#endregion

		#region Constructor(s)

		public ItemPolicy(string groupUserName)
		{
			this.groupUserName = groupUserName;
			this.roles = new List<ItemPolicyRole>();
		}

		#endregion

		#region Public Properties

		public string GroupUserName
		{
			get
			{
				return this.groupUserName;
			}
		}

		public IList<ItemPolicyRole> Roles
		{
			get
			{
				return this.roles;
			}
		}

		#endregion

		#region IComparable Members

		public int CompareTo(object obj)
		{
			ItemPolicy other = obj as ItemPolicy;
			if ( other != null )
			{
				return this.GroupUserName.CompareTo(other.GroupUserName);
			}
			else
			{
				return -1;
			}
		}

		#endregion

		#region IComparable<ItemPolicy> Members

		public int CompareTo(ItemPolicy other)
		{
			return this.GroupUserName.CompareTo(other.GroupUserName);
		}

		#endregion

		public override bool Equals(object obj)
		{
			ItemPolicy other = obj as ItemPolicy;
			if ( other != null )
			{
				if ( !this.GroupUserName.Equals(other.GroupUserName) )
				{
					return false;
				}
				if ( other.Roles.Count != this.Roles.Count )
				{
					return false;
				}
				else
				{
					List<ItemPolicyRole> listA = new List<ItemPolicyRole>(this.Roles);
					List<ItemPolicyRole> listB = new List<ItemPolicyRole>(other.Roles);
					for (int x = 0; x < listA.Count; x++ )
					{
						if ( string.Compare(listA[x].Name, listB[x].Name, false) != 0 )
						{
							return false;
						}
					}
				}
				return true;
			}
			else
			{
				return false;
			}
		}

		public override int GetHashCode()
		{
			return this.GroupUserName.GetHashCode();
		}
	}
}